#include "stockviewwidget.h"
#include "ui_stockviewwidget.h"

#include <QDate>
#include <QSqlError>
StockViewWidget::StockViewWidget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::StockViewWidget)
    , model(new QSqlQueryModel(this))
{
    ui->setupUi(this);
    setWindowTitle("库存信息");

    // 设置表格模型
    ui->tableView->setModel(model);

    // 设置表格样式
    setupTable();

    // 连接刷新按钮
    connect(ui->pbRefresh, &QPushButton::clicked, this, &StockViewWidget::onRefresh);

    // 加载库存数据
    loadStockData();
}

StockViewWidget::~StockViewWidget()
{
    delete ui;
}

void StockViewWidget::loadStockData()
{
    //加载状态
    ui->lblStatus->setText("正在加载库存数据...");
    QDateTime starTime = QDateTime::currentDateTime();

    //查询数据库数据
    model->setQuery(
        "SELECT "
        "w.warehouse_name AS '仓库名称', "
        "p.name AS '产品名称', "
        "p.spec AS '规格', "
        "p.unit AS '单位', "
        "s.quantity AS '库存数量', "
        "s.lock_quantity AS '锁定数量', "
        "s.stock_status AS '库存状态', "
        "s.last_updated AS '最后更新时间' "
        "FROM Stock s "
        "JOIN Warehouse w ON s.warehouse_id = w.warehouse_id "
        "JOIN Product p ON s.product_id = p.product_id "
        "ORDER BY w.warehouse_name, p.name"
        );

    //检查错误
    if (model->lastError().isValid())
    {
        ui->lblStatus->setText("数据加载失败" + model->lastError().text());
        return;
    }

    // 设置列标题
    model->setHeaderData(0, Qt::Horizontal, "仓库名称");
    model->setHeaderData(1, Qt::Horizontal, "产品名称");
    model->setHeaderData(2, Qt::Horizontal, "规格");
    model->setHeaderData(3, Qt::Horizontal, "单位");
    model->setHeaderData(4, Qt::Horizontal, "库存数量");
    model->setHeaderData(5, Qt::Horizontal, "锁定数量");
    model->setHeaderData(6, Qt::Horizontal, "库存状态");
    model->setHeaderData(7, Qt::Horizontal, "最后更新时间");

    // 调整列宽
    for (int i = 0; i < model->columnCount(); i++)
    {
        ui->tableView->resizeColumnToContents(i);
    }

    //显示记录数和加载时间
    int rowCount = model->rowCount();
    qint64 elapsed = starTime.msecsTo(QDateTime::currentDateTime());
    ui->lblStatus->setText(QString("共 %1 条库存记录 | 加载时间: %2 毫秒").arg(rowCount, elapsed));

    //添加时间戳
    ui->lblLastUpdate->setText(QString("数据更新时间: %1").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")));
}

void StockViewWidget::setupTable()
{
    // 设置表格属性
    ui->tableView->setAlternatingRowColors(true);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableView->verticalHeader()->setVisible(false);
    ui->tableView->setSortingEnabled(true);

    // 设置表格样式
    ui->tableView->setStyleSheet(
        "QTableView::item {"
        "   padding: 8px;"
        "}"
        "QTableView::item[stock_status='预警'] {"
        "   background-color: #fff9c4;"
        "   color: #f57c00;"
        "   font-weight: bold;"
        "}"
        "QTableView::item[stock_status='冻结'] {"
        "   background-color: #ffebee;"
        "   color: #d32f2f;"
        "   font-weight: bold;"
        "}"
        );
}

void StockViewWidget::onRefresh()
{
    loadStockData();
}
